<?php
namespace app\sub\controller;

use think\Db;
use think\facade\Config;

class Admin extends \Common {

    # 秘钥随机字段
    private $arr = [16,12,11,13,15,14];
    private $brr = [0,1,2,3,4];
    private $key = "^&*$#";

    # 登录
    public function Login(){
        (new \app\super\controller\Admin())->Login();
    }
    # 获取信息
    public function Info(){
        (new \app\super\controller\Admin())->Info();
    }
    # 注销
    public function Logout(){
        (new \app\super\controller\Admin())->Logout();
    }

    # 修改次级管理员密码
    public function PassWord(){
        $aid = $this->token["id"];
        if(isset($_POST["oldp"]) && isset($_POST["newp"])){
            $oldp = $_POST["oldp"];
            $newp = $_POST["newp"];
            $admin = Db::name("admin")->where("aid", $aid)->find();
            if(!empty($admin)){
                $old_password = base64_decode($admin["password64"]);
                if($oldp == $old_password){
                    $update = [
                        "password" => md5($newp),
                        "password64" => base64_encode($newp)
                    ];
                    Db::name("admin")->where("aid", $aid)->update($update);
                    Code(200, "修改成功，请用新密码重新登入");
                }else{
                    Code(403, "旧密码错误");
                }
            }else{
                Code(403, "服务器错误，请稍后再试");
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    # 获取LOGO和简介
    public function GetLogo(){
        $aid = $this->token["id"];
        $admin = Db::name("admin")->where("aid", $aid)->find();
        $array = [
            "intro" => $admin["intro"],
            "logo" => $admin["logo"] == "" ? "" : Config::get("app.ip")."static/upload/logo/{$admin["logo"]}?t=".time()
        ];

        Code(200, false, $array);
    }

    # 设置LOGO和简介
    public function SetLogo(){
        $aid = $this->token["id"];
        $update = [];
        if(isset($_FILES["logo"])){
            $update["logo"] = UploadFile($_FILES["logo"], $aid, "./static/upload/logo/");
        }
        if(isset($_POST["intro"])){
            $update["intro"] = $_POST["intro"];
        }
        Db::name("admin")->where("aid", $aid)->update($update);
        $admin = Db::name("admin")->where("aid", $aid)->find();
        $array = [
            "intro" => $admin["intro"],
            "logo" => $admin["logo"] == "" ? "" : Config::get("app.ip")."static/upload/logo/{$admin["logo"]}?t=".time()
        ];

        Code(200, "信息设置成功", $array);
    }

    # 秘钥生成
    public function Exportkeys(){
        $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        if(isset($_POST["start"]) && isset($_POST["end"])){
            $start = $_POST["start"];
            $end = $_POST["end"];

            $max = Db::name("num")->where("aid", $aid)->order("number desc")->find();
            $max_number = empty($max) ? -1 : $max["number"];
            for ($i = $start; $i <= $end; $i++){
                $rand = rand(0000,9999);
                $string = $this->key.$rand.$i.msectime();
                $md5 = md5($string);
                $key = strtoupper(substr($md5,$this->brr[array_rand($this->brr)],$this->arr[array_rand($this->arr)]));
                $add = [
                    'number' => $i,
                    'password' => $key,
                    'ub_id' => 0,
                    "aid" => $aid
                ];
                if($i > $max_number){
                    Db::name("num")->insert($add);
                }else{
                    Db::name("num")->where("aid", $aid)->where("number",$i)->update($add);
                }
            }
            $num_start = Db::name("num")->where("aid", $aid)->order("number asc")->find();
            $num_end = Db::name("num")->where("aid", $aid)->order("number desc")->find();
            if(!empty($num_start)){
                $new_name = $num_start["number"]."-".$num_end["number"]."号牌秘钥";
            }else{
                $new_name = "号牌秘钥";
            }
            $file = $this->GetNumExcel($new_name, $aid);
            $array = [
                "name" => !$file ? "" : "$new_name.xlsx",
                "url" => !$file ? "" : $file,
                "max" => (int)$end
            ];
            Code(200, "秘钥生成成功,点击下载", $array);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    public function DownloadKeys(){
        $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        $num_start = Db::name("num")->where("aid", $aid)->order("number asc")->find();
        $num_end = Db::name("num")->where("aid", $aid)->order("number desc")->find();
        if(!empty($num_start)){
            $new_name = $num_start["number"]."-".$num_end["number"]."号牌秘钥";
        }else{
            $new_name = "号牌秘钥";
        }
        $file = $this->GetNumExcel($new_name, $aid);
        $array = [
            "name" => !$file ? "" : "$new_name.xlsx",
            "url" => !$file ? "" : $file,
        ];

        Code(200, false, $array);
    }

    public function GetKeys(){
        $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        if(isset($_GET["page"]) && isset($_GET["limit"])){
            $page = $_GET["page"];
            $limit = $_GET["limit"];
            if(isset($_GET["bind"])){
                switch ($_GET["bind"]){
                    case 0 :
                        $num = Db::name("num")
                            ->where("aid", $aid)
                            ->where("ub_id", 0)
                            ->order("number asc")
                            ->page($page, $limit)
                            ->select();
                        $total = Db::name("num")
                            ->where("aid", $aid)
                            ->where("ub_id", 0)
                            ->count();
                        break;
                    default :
                        $num = Db::name("num")
                            ->where("aid", $aid)
                            ->where("ub_id != 0")
                            ->order("number asc")
                            ->page($page, $limit)
                            ->select();
                        $total = Db::name("num")
                            ->where("aid", $aid)
                            ->where("ub_id != 0")
                            ->count();
                        break;
                }
            }else{
                $num = Db::name("num")
                    ->where("aid", $aid)
                    ->order("number asc")
                    ->page($page, $limit)
                    ->select();
                $total = Db::name("num")
                    ->where("aid", $aid)
                    ->count();
            }

            $arr = [];
            if(!empty($num)){
                foreach ($num as $value){
                    $arr[] = [
                        "id" => (int)$value["nid"],
                        "is_bind" => $value["ub_id"] != 0,
                        "key" => $value["password"],
                        "num" => $value["number"]
                    ];
                }
            }

            $array = [
                "list" => $arr,
                "total" => $total
            ];
            Code(200, false, $array);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    # 获取代拍账户
    public function GetBidCos(){
        (new \app\super\controller\Admin())->GetBidCos();
    }

    # 获取代拍账户下拉
    public function GetAllBidCos(){
        $aid = isset($this->token["pid"]) ? $this->token["pid"] : $this->token["id"];
        $company = Db::name("user_proxy_company")->alias("a")->join("user_proxy b", "a.up_id = b.up_id")->where("a.aid", $aid)->select();
        $data = [];
        foreach ($company as $value){
            $data[] = [
                "id" => $value["up_id"],
                "name" => $value["name"]
            ];
        }

        Code(200, false, $data);
    }

    public function UpdateBidCo(){
        (new \app\super\controller\Admin())->UpdateBidCo();
    }

}
